/*
 * 
 * 
 * @author: Oren Zamir
 */
package org.valire.usermanagement.services;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.valire.usermanagement.beans.MethodAccessControl;
import org.valire.usermanagement.beans.Role;
import org.valire.util.lang.Args;


/**
 * The Class AccessControlMethodServiceImpl.
 */
public class MethodAccessControlServiceImpl implements
		MethodAccessControlService {
	
	/** The Constant logger. */
	@SuppressWarnings("unused")
	private final static Log logger = LogFactory.getLog(MethodAccessControlServiceImpl.class);

	/** The roles dao. */
	@Resource
	private MethodAccessControlDao macDao;

	/**
	 * Sets the user dao.
	 *
	 * @param macDao the new mac dao
	 */
	public void setMacDao(MethodAccessControlDao macDao) {
		this.macDao = macDao;
	}

	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.MethodAccessControlService#createMAC(org.valire.usermanagement.beans.MethodAccessControl)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	@PreAuthorize("hasPermission(#mac, 'org.valire.usermanagement.services.MethodAccessControlService.createMAC(mac)')")
	public MethodAccessControl createMAC(MethodAccessControl mac) {
		Args.assertNotNull(mac);
		this.macDao.create(mac);
		return mac;
	}

	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.MethodAccessControlService#removeMAC(org.valire.usermanagement.beans.MethodAccessControl)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	@PreAuthorize("hasPermission(#mac, 'org.valire.usermanagement.services.MethodAccessControlService.removeMAC(mac)')")
	public void removeMAC(MethodAccessControl mac) {
		Args.assertNotNull(mac);
		this.macDao.setPersistentClass(MethodAccessControl.class);
		this.macDao.delete(mac.getId());
	}
	
	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.MethodAccessControlService#updateMAC(org.valire.usermanagement.beans.MethodAccessControl)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	@PreAuthorize("hasPermission(#mac, 'org.valire.usermanagement.services.MethodAccessControlService.updateMAC(mac)')")
	public void updateMAC(MethodAccessControl mac) {
		Args.assertNotNull(mac.getId());
		Args.assertNotNull(mac.getMethodName());
		macDao.merge(mac);		
	}
	
	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.MethodAccessControlService#getAllMethodsAccessControl()
	 */
	@Override
	public List<MethodAccessControl> getAllMethodsAccessControl() {
		return this.macDao.getAllAvailableMethods();
	}

	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.MethodAccessControlService#getMethodsForRoles(java.util.Set)
	 */
	@Override
	public Set<MethodAccessControl> getMethodsForRoles(Set<Role> roles) {
		return this.macDao.getMethodsForRoles(roles);
	}

	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.MethodAccessControlService#checkMethodAccessForRole(org.valire.usermanagement.beans.Role, java.lang.String)
	 */
	@Override
	public boolean checkMethodAccessForRole(Role role, String method) {
		return this.macDao.canRoleAccessMethod(role, method);
	}

	}
